﻿/**
 * ...
 * @author sminutoli
 */

import com.dmotiko.meip.*
import com.dmotiko.general.*
import flash.external.ExternalInterface;
import mx.utils.Delegate;
 
class com.dmotiko.meip.BindLibrary 
{
	
	private var mcHeader:BaseClip;
	private var mcFooter:FooterClip;
	
	private var section_listener:Function;
	private var section_hidden_listener:Function;
	private var section_showed_listener:Function;
	private var sections:Array;
	private var mcHome:Section;
	private var mcContacto:Section;
	private var mcClientes:Section;
	private var mcProducts:Section;
	private var mcNosotros:Section;
	private var mcObras:Section;
	private var mcBack:BackMain;
	
	public function BindLibrary(timeline:MovieClip){
				
		mcHeader = BaseClip( timeline.attachMovie("library.HeaderBar", "headerBar", 100) );
		mcFooter = FooterClip( timeline.attachMovie("library.FooterBar", "footerBar", 99) );
		mcBack = BackMain( timeline.attachMovie("library.BackMain", "backMain", -100) );
		
		//sections
		mcHome = Section( timeline.attachMovie("library.HomeSection", "homeSection", 1, { data: SiteSections.HOME } ) );
		mcContacto = Section( timeline.attachMovie("library.ContactoSection", "contactoSection", 2, { data: SiteSections.CONTACTO }) );
		mcNosotros = Section( timeline.attachMovie("library.NosotrosSection", "nosotrosSection", 3, { data: SiteSections.NOSOTROS }) );
		mcClientes = Section( timeline.attachMovie("library.ClientesSection", "clientesSection", 4, { data: SiteSections.CLIENTES }) );
		mcProducts = Section( timeline.attachMovie("library.ProductosSection", "productsSection", 5, { data: SiteSections.PRODUCTOS } ) );
		mcObras = Section( timeline.attachMovie("library.ObrasSection", "obrasSection", 6, { data: SiteSections.OBRAS }) );
		sections = [ mcHome, mcContacto, mcNosotros, mcClientes, mcProducts, mcObras ];
		
		//init
		mcFooter._x = 3;
		mcFooter._y = 443;
		mcBack._y = 74;
		mcBack._x = 3;
		
		mcHome._x = 3;
						
		//events
		mcHeader["btn_logo"].onRelease = function() { Site.getApp().setSection( SiteSections.HOME ) }
		mcHeader["btn_home"].onRelease = function() { Site.getApp().setSection( SiteSections.HOME ); }
		mcHeader["btn_home"].gotoAndStop(2);
		mcHeader["btn_home"].onRollOut = function() {
			if ( Site.getApp().getSection() == SiteSections.HOME ) return;
			this.gotoAndStop( "normal" );
		}
		
				
		mcHeader["btn_nosotros"].onRelease = function() { Site.getApp().setSection( SiteSections.NOSOTROS ) }
		mcHeader["btn_productos"].onRelease = function() { Site.getApp().setSection( SiteSections.PRODUCTOS ) }
		mcHeader["btn_obras"].onRelease = function() { Site.getApp().setSection( SiteSections.OBRAS ) }
		mcHeader["btn_clientes"].onRelease = function() { Site.getApp().setSection( SiteSections.CLIENTES ) }
		mcHeader["btn_contacto"].onRelease = function() { Site.getApp().setSection( SiteSections.CONTACTO ) }
				
		mcFooter["banner_productos"].onRelease = function() { 
			Site.getApp().setSection( SiteSections.PRODUCTOS );
			//ExternalInterface.call("scrollWindowTo", 0);
		}
		mcFooter["banner_obras"].onRelease = function() { 
			Site.getApp().setSection( SiteSections.OBRAS ) 
			//ExternalInterface.call("scrollWindowTo", 0);
		}
				
		//list
		section_listener = Delegate.create( this, section_changed );
		section_hidden_listener = Delegate.create( this, section_hidden);
		section_showed_listener = Delegate.create( this, section_showed);
		
		Site.getApp().addEventListener( WebSite.SECTION_CHANGED, section_listener);
		for ( var i:Number = 0; i < sections.length; i++ ) {
			sections[i].addEventListener( BaseClipEvent.HIDE_END, section_hidden_listener);
			sections[i].addEventListener( BaseClipEvent.SHOW_END, section_showed_listener);
		}
	}
	
	private function section_showed(e):Void 
	{
		switch (e.target) 
		{
			case mcNosotros:
			e.target.mcTop.onRelease = function() {
				ExternalInterface.call("scrollWindowTo", 0);
			}
			break;
			
			case mcClientes:
			e.target.mcTop.onRelease = function() {
				ExternalInterface.call("scrollWindowTo", 0);
			}
			break;
		}
	}
	
	private function section_hidden():Void 
	{
		for ( var i:Number = 0; i < sections.length; i++ ) {
			var section:BaseClip = sections[i];
			if ( Site.getApp().getSection() == section.getData() ) {
				section.show();
				mcFooter.moveY( section.getBounds(_root).yMax );
				mcBack.scaleY( section.getBounds(_root).yMax - mcBack._y );
			}
		}
	}
	
	private function section_changed(e):Void {
		
		if ( e.section == SiteSections.HOME ) {
			mcHeader["btn_home"].onRollOver();
			mcHeader["btn_home"].setActive(true, true);
		} else {
			mcHeader["btn_home"].onRollOut();
			mcHeader["btn_home"].setActive(false, true);
		}
		var b:Boolean;
		for ( var i:Number = 0; i < sections.length; i++ ) {
			var section:BaseClip = sections[i];
			section.hide();
			if ( section.isShowed() ) b = true;
		}
		if ( !b ) section_hidden();
	}
	
	public function getHome():Section {	return mcHome; }
	public function getProducto():Section {	return mcProducts; }
	public function getHeader():BaseClip {	return mcHeader; }
	public function getFooter():BaseClip {	return mcFooter; }
	public function getBack():BackMain {	return mcBack; }
	
}